package com.zhuoyue.web.controller;

import com.zhuoyue.biz.core.api.dto.rep.LinkageRelationRepDTO;
import com.zhuoyue.biz.core.api.dto.req.LinkageRelationReqDTO;
import com.zhuoyue.biz.core.api.service.impl.RelationDeviceService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.dto.ControllerResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjming
 * @ClassName: AppLinkAcSetController
 * @Description: 空调联控(实际是联动1777)
 * @Date 2021-08-06 17:36
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/relation")
@Validated
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppLinkAcSetController {
    private final RelationDeviceService relationDeviceService;

    @GetMapping("/get")
    @CheckLogin
    public ControllerResult getRelationMessage(@NotBlank(message = "开始设备不能为空") String startDeviceId) {
        LinkageRelationRepDTO rep = relationDeviceService.getByStartId(startDeviceId);
        return ControllerResult.toReturnDataSuccess("获取成功", rep);
    }

    /**
     * 双向联控
     *
     * @param linkReq
     * @return
     */
    @PostMapping("/add/ac")
    @CheckLogin
    public ControllerResult addAc(@RequestBody LinkageRelationReqDTO linkReq) {
        LinkageRelationRepDTO rep = relationDeviceService.addRelation(linkReq);
        return ControllerResult.toReturnDataSuccess("添加成功", rep);
    }

    /**
     * 单项联控
     *
     * @param linkReq
     * @return
     */
    @PostMapping("/add/zigbee/ac")
    @CheckLogin
    public ControllerResult addZigbeeAc(@RequestBody LinkageRelationReqDTO linkReq) {
        LinkageRelationRepDTO rep = relationDeviceService.addZigbeeRelation(linkReq);
        return ControllerResult.toReturnDataSuccess("添加成功", rep);
    }

    @DeleteMapping("/delete/ac")
    @CheckLogin
    public ControllerResult deleteAc(@NotBlank(message = "id不能为空") String id) {
        relationDeviceService.removeById(id);
        return ControllerResult.toReturnMessageSuccess("删除成功");
    }

    @DeleteMapping("/clear")
    @CheckLogin
    public ControllerResult clear(@NotBlank(message = "id不能为空") String startDeviceId) {
        relationDeviceService.removeAll(startDeviceId);
        return ControllerResult.toReturnMessageSuccess("清空成功");
    }

}
